package lab;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WestPanel extends JPanel {

    Cards cards = new Cards(null);
    MouseClicks mouseClicks = new MouseClicks();
    KeyStrokes keyStrokes = new KeyStrokes();
    JLabel thriller = new JLabel("Thriller");
    JLabel horror = new JLabel("Horror");
    JLabel fantasy = new JLabel("Fantasy");
    JLabel children = new JLabel("Children");
    JLabel mystery = new JLabel("Mystery");
    JLabel romantic = new JLabel("Romantic");
    JTextField search = new JTextField("Search...", 20);
    String searchText;

    public WestPanel(Cards cards) {
        this.cards = cards;
        search.setPreferredSize(new Dimension(180, 20));
        search.setMaximumSize(search.getPreferredSize());
        search.setFocusable(false);
        add(search);
        add(thriller);
        add(horror);
        add(fantasy);
        add(children);
        add(mystery);
        add(romantic);

        thriller.addMouseListener(mouseClicks);
        horror.addMouseListener(mouseClicks);
        fantasy.addMouseListener(mouseClicks);
        children.addMouseListener(mouseClicks);
        mystery.addMouseListener(mouseClicks);
        romantic.addMouseListener(mouseClicks);
        search.addMouseListener(mouseClicks);
        search.addKeyListener(keyStrokes);
    }

    private class MouseClicks implements MouseListener {

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getComponent().equals(thriller)) {
                cards.searchGenreOnly("thriller");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(horror)) {
                cards.searchGenreOnly("horror");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(fantasy)) {
                cards.searchGenreOnly("fantasy");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(children)) {
                cards.searchGenreOnly("children");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(mystery)) {
                cards.searchGenreOnly("mystery");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(romantic)) {
                cards.searchGenreOnly("romantic");
                cards.cl.show(cards.cardPanel, "1");
            } else if (e.getComponent().equals(search)) {
                search.setFocusable(true);
                search.setText("");
                search.grabFocus();
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }

    private class KeyStrokes implements KeyListener {

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (search.isFocusOwner()) {
                if (keyCode == KeyEvent.VK_ENTER) {
            searchText = search.getText();
            if(!searchText.isEmpty()){
                cards.searchAnything(searchText);
                cards.cl.show(cards.cardPanel, "1");
            }else{
                System.out.println("Enter something");
            }
            
                }
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
        }
    }
}
